package org.jeecg.modules.scm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.scm.constant.ScmConstant;
import org.jeecg.modules.scm.entity.ScmChainFinaContract;
import org.jeecg.modules.scm.entity.ScmCloudChain;
import org.jeecg.modules.scm.entity.ScmFinaContract;
import org.jeecg.modules.scm.entity.ScmReceivables;
import org.jeecg.modules.scm.mapper.ScmChainFinaContractMapper;
import org.jeecg.modules.scm.mapper.ScmCloudChainMapper;
import org.jeecg.modules.scm.mapper.ScmFinaContractMapper;
import org.jeecg.modules.scm.service.IScmCloudChainService;
import org.jeecg.modules.scm.service.IScmReceivablesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: scm_cloud_chain
 * @Author: jeecg-boot
 * @Date: 2021-11-04
 * @Version: V1.0
 */
@Service
@Slf4j
public class ScmCloudChainServiceImpl extends ServiceImpl<ScmCloudChainMapper, ScmCloudChain> implements IScmCloudChainService {

    @Autowired
    private IScmReceivablesService scmReceivablesService;
    @Resource
    private ScmCloudChainMapper scmCloudChainMapper;
    @Resource
    private ScmChainFinaContractMapper scmChainFinaContractMapper;
    @Resource
    private ScmFinaContractMapper scmFinaContractMapper;


    @Override
    public ScmFinaContract queryFinaContractById(String id) {
        LambdaQueryWrapper<ScmChainFinaContract> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(ScmChainFinaContract::getApplyNo, id);
        ScmChainFinaContract scmChainFinaContract = scmChainFinaContractMapper.selectOne(queryWrapper1);
        if (scmChainFinaContract == null){
            return null;
        }
        LambdaQueryWrapper<ScmFinaContract> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(ScmFinaContract::getApplyNo, scmChainFinaContract.getApplyNo());
        ScmFinaContract scmFinaContract = scmFinaContractMapper.selectOne(queryWrapper2);
        return scmFinaContract;
    }

    @Override
    public List<ScmCloudChain> queryByReceivables(Integer receivablesId) {
        LambdaQueryWrapper<ScmCloudChain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ScmCloudChain::getOriginalReceivableNo, receivablesId);
        List<ScmCloudChain> scmCloudChainList = scmCloudChainMapper.selectList(queryWrapper);
        return scmCloudChainList;
    }

    @Override
    public String onBlockchain(Integer receivablesId) {
        ScmReceivables receivables = scmReceivablesService.getById(receivablesId);
        if (receivables == null) {
            log.error("上链时应收账款信息为空,receivablesId:{}", receivablesId);
            return null;
        }
        // 修改应收账款的状态为上链中。。。。。
        boolean isOk = scmReceivablesService.updateStatusById(ScmConstant.RECEIVABLES_STATUS_ON_CHAIN_PREPARE, receivablesId);
        if (!isOk) {
            log.info("上链时修改应收账款信息状态失败,receivablesId:{}", receivablesId);
        }
        ScmCloudChain cloudChain = new ScmCloudChain();
        cloudChain.setOwnerCusNo(receivables.getOriginalCreditorCusNo());
        cloudChain.setDebtorCusNo(receivables.getOriginalDebtorCusNo());
        cloudChain.setAmount(receivables.getAmount());
        cloudChain.setCurrency(receivables.getCurrency());
        cloudChain.setOriginalReceivableNo(receivables.getId());
        cloudChain.setOriginalContractNo(receivables.getContractNo());

        cloudChain.setDueTime(receivables.getDueTime());
        // TODO 原始交易合同信息待填、云链单信息。。。

        int insert = baseMapper.insert(cloudChain);
        if (insert < 1) {
            log.info("上链时插入链单数据失败,receivablesId:{}", receivablesId);
        }

        // TODO 调用区块链上链接口
        this.queryOnBlockchain(cloudChain);
        return cloudChain.getChainNo();
    }

    /**
     * 模拟上链
     *
     * @param scmCloudChain
     */
    void queryOnBlockchain(ScmCloudChain scmCloudChain) {

        // 上链得到的hash
        String hash = "hshkjacjsflsiekgscnsvnklafklafla";
        // 成功回调
        boolean b = rollbackOnBlockchain(hash, scmCloudChain.getChainNo(), scmCloudChain.getOriginalReceivableNo());
    }

    /**
     * 上链成功后回调
     *
     * @return
     */
    boolean rollbackOnBlockchain(String hash, String chainNo, Integer receivablesId) {
        boolean isOk = lambdaUpdate().eq(ScmCloudChain::getChainNo, chainNo)
                .set(ScmCloudChain::getBlockChain, hash)
                .set(ScmCloudChain::getStauts, 1)
                .update();
        if (!isOk) {
            log.info("上链成功后回调修改云链单时失败,receivablesId:{},chainNo:{}", receivablesId, chainNo);
            return false;
        }

        boolean b = scmReceivablesService.updateStatusById(ScmConstant.RECEIVABLES_STATUS_ON_CHAIN, receivablesId);
        if (!b) {
            log.info("上链成功后回调修改应收账款时失败,receivablesId:{},chainNo:{}", receivablesId, chainNo);
            return false;
        }
        return true;
    }

}
